#include "edittaskdlg.h"
#include "router/systemMonitor/timedTask/cronexpresscreatedlg.h"
#include "ui_edittaskdlg.h"

#include <QMouseEvent>

EditTaskDlg::EditTaskDlg(qlonglong id,QList<DictData> sys_job_group,QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::EditTaskDlg)
{
    ui->setupUi(this);
    setWindowIcon(QIcon(":/image/res/stm.jpg"));
    QPixmap what(":/svg/res/svg/question.svg");
    what = what.scaled(15,15);
    ui->label_what->setPixmap(what);
    ui->label_what->setToolTip("Bean调用示例：ryTask.ryParams('ry')\n"
                               "Class类调用示例：com.ruoyi.quartz.task.RyTask.ryParams('ry')\n"
                               "参数说明：支持字符串，布尔类型，长整型，浮点型，整型\n");

    job.jobId = id;
    this->sys_job_group = sys_job_group;
    job.concurrent = "1";
    job.misfirePolicy = "1";
    if(id > 0){
        ui->label->setText("编辑任务");
    }

    init();

}

EditTaskDlg::~EditTaskDlg()
{
    delete ui;
}

bool EditTaskDlg::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type() == QEvent::MouseButtonPress){
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
        if(mouseEvent->button() == Qt::LeftButton){
            return true;
        }else{
            return false;
        }
    }else if(event->type() == QEvent::Enter){
        return true;
    }else if(event->type() == QEvent::Leave){
        return true;
    }else{
        return QObject::eventFilter(obj, event);
    }
}

void EditTaskDlg::init()
{
    for (int i = 0; i < sys_job_group.size(); ++i) {
        ui->comboBox_jobGroup->addItem(sys_job_group.at(i).dictLabel,sys_job_group.at(i).dictValue);
    }
    if(job.jobId > 0){
        QByteArray *data = api.getById(job.jobId);
        QJsonObject obj = QJsonDocument::fromJson(*data).object();
        if(obj.value("code").toInt() != 200){
            return;
        }
        job = Job::toJob(obj.value("data").toObject());
        ui->lineEdit_jobName->setText(job.jobName);
        for (int i = 0; i < ui->comboBox_jobGroup->count(); ++i) {
            if(job.jobGroup == ui->comboBox_jobGroup->itemData(i).value<QString>()){
                ui->comboBox_jobGroup->setCurrentIndex(i);
                break;
            }
        }
        ui->lineEdit_invokeMethod->setText(job.invokeTarget);
        ui->lineEdit_cronExpression->setText(job.cronExpression);
        if(job.misfirePolicy == "2"){
            ui->btn_executeOnce->clicked();
        }else if(job.misfirePolicy == "3"){
            ui->btn_executeNone->clicked();
        }
        if(job.concurrent == "0"){
            ui->btn_concurrent->click();
        }else if(job.status == "1"){
            ui->radioButton_pause->setChecked(1);
        }
    }
}

void EditTaskDlg::on_btn_close_clicked()
{
    close();
}


void EditTaskDlg::on_btn_executeNow_clicked()
{
    job.misfirePolicy = "1";
    ui->btn_executeNow->setStyleSheet("QPushButton {color:white;background-color: rgb(24, 144, 255);border:1px solid gray;}");
    ui->btn_executeOnce->setStyleSheet("QPushButton:hover {color: rgb(24, 144, 255); /* 设置按钮的字体颜色 */}QPushButton {background-color: rgb(255, 255, 255);border:1px solid gray;}");
    ui->btn_executeNone->setStyleSheet("QPushButton:hover {color: rgb(24, 144, 255); /* 设置按钮的字体颜色 */}QPushButton {background-color: rgb(255, 255, 255);border:1px solid gray;}");
}


void EditTaskDlg::on_btn_executeOnce_clicked()
{
    job.misfirePolicy = "2";
    ui->btn_executeNow->setStyleSheet("QPushButton:hover {color: rgb(24, 144, 255); /* 设置按钮的字体颜色 */}QPushButton {background-color: rgb(255, 255, 255);border:1px solid gray;}");
    ui->btn_executeOnce->setStyleSheet("QPushButton {color:white;background-color: rgb(24, 144, 255);border:1px solid gray;}");
    ui->btn_executeNone->setStyleSheet("QPushButton:hover {color: rgb(24, 144, 255); /* 设置按钮的字体颜色 */}QPushButton {background-color: rgb(255, 255, 255);border:1px solid gray;}");
}


void EditTaskDlg::on_btn_executeNone_clicked()
{
    job.misfirePolicy = "3";
    ui->btn_executeNow->setStyleSheet("QPushButton:hover {color: rgb(24, 144, 255); /* 设置按钮的字体颜色 */}QPushButton {background-color: rgb(255, 255, 255);border:1px solid gray;}");
    ui->btn_executeOnce->setStyleSheet("QPushButton:hover {color: rgb(24, 144, 255); /* 设置按钮的字体颜色 */}QPushButton {background-color: rgb(255, 255, 255);border:1px solid gray;}");
    ui->btn_executeNone->setStyleSheet("QPushButton {color:white;background-color: rgb(24, 144, 255);border:1px solid gray;}");
}


void EditTaskDlg::on_btn_concurrent_clicked()
{
    job.concurrent = "0";
    ui->btn_concurrent->setStyleSheet("QPushButton {color:white;background-color: rgb(24, 144, 255);border:1px solid gray;}");
    ui->btn_noConcurrent->setStyleSheet("QPushButton:hover {color: rgb(24, 144, 255); /* 设置按钮的字体颜色 */}QPushButton {background-color: rgb(255, 255, 255);border:1px solid gray;}");
}


void EditTaskDlg::on_btn_noConcurrent_clicked()
{
    job.concurrent = "1";
    ui->btn_concurrent->setStyleSheet("QPushButton:hover {color: rgb(24, 144, 255); /* 设置按钮的字体颜色 */}QPushButton {background-color: rgb(255, 255, 255);border:1px solid gray;}");
    ui->btn_noConcurrent->setStyleSheet("QPushButton {color:white;background-color: rgb(24, 144, 255);border:1px solid gray;}");
}


void EditTaskDlg::on_btn_ok_clicked()
{
    if(ui->lineEdit_jobName->text().isEmpty()){
        ui->lineEdit_jobName->setStyleSheet("border:1px solid red;");
    }else{
        ui->lineEdit_jobName->setStyleSheet("");
    }
    if(ui->lineEdit_invokeMethod->text().isEmpty()){
        ui->lineEdit_invokeMethod->setStyleSheet("border:1px solid red;");
    }else{
        ui->lineEdit_invokeMethod->setStyleSheet("");
    }
    if(ui->lineEdit_cronExpression->text().isEmpty()){
        ui->lineEdit_cronExpression->setStyleSheet("border:1px solid red;");
    }else{
        ui->lineEdit_cronExpression->setStyleSheet("");
    }
    job.jobName = ui->lineEdit_jobName->text();
    job.jobGroup = ui->comboBox_jobGroup->currentData().value<QString>();
    job.invokeTarget = ui->lineEdit_invokeMethod->text();
    job.cronExpression = ui->lineEdit_cronExpression->text();
    job.status = ui->radioButton_normal->isChecked() ? "0" : "1";
    QByteArray *data = api.editJob(job);
    QJsonObject obj = QJsonDocument::fromJson(*data).object();
    if(obj.value("code").toInt() == 200){
        QMessageBox::warning(0,"提示",obj.value("msg").toString());
        emit success();
        close();
    }
}


void EditTaskDlg::on_pushButton_clicked()
{
    CronExpressCreateDlg *c = new CronExpressCreateDlg;
    c->setAttribute(Qt::WA_DeleteOnClose);
    c->exec();
}

